package com.jxb.test20241216;

import java.util.ArrayList;
import java.util.List;

/**
 * 示例 1：
 * 输入：nums = [4,3,2,7,8,2,3,1]
 * 输出：[5,6]
 * 示例 2：
 * 输入：nums = [1,1]
 * 输出：[2]
 */
public class NumbersDisappeared_448 {

    public static void main(String[] args) {
        int[] nums = {4,3,2,7,8,2,3,1};
        System.out.println(findNumbersDisappeared1(nums));

    }
    //[4,3,2,7,8,2,3,1]-->[-4,-3,-2,-7,8,2,-3,-1]
    public static List<Integer> findNumbersDisappeared(int[] nums) {
        List<Integer> res =  new ArrayList<>();
        //将元素变为负数
        for (int i = 0; i < nums.length;i++) {
            int index = Math.abs(nums[i]) - 1;
            if (nums[index] > 0) {
                nums[index] = - nums[index];
            }
        }

        //将负数过滤，元素为正数的是缺少的元素
        for (int i = 0; i < nums.length;i++) {
            if (nums[i] > 0) {
                res.add(i+1);
            }
        }
        return res;
    }

    //将每个元素进行-1取模加上数组长度，最后进行过滤，若小于等于数组长度的就是缺少的元素
    //4-->{4,3,2,15,8,2,3,1}
    //3-->{4,3,10,15,8,2,3,1}
    //2-->{4,11,10,15,8,2,3,1}
    //7-->{4,11,10,15,8,2,11,1}
    //8-->{4,11,10,15,8,2,11,9}
    //2-->{4,11,10,15,8,2,11,9}
    //3-->{4,11,10,15,8,2,11,9}
    //1-->{12,11,10,15,8,2,11,9}
    //int x = (1 - 1) %8;
    //int i = nums[x] += 8;
    public static List<Integer> findNumbersDisappeared1(int[] nums) {
        List<Integer> res = new ArrayList<>();
        int length = nums.length;
        //将每个元素进行-1取模加上数组长度
        for (Integer num : nums) {
            int x = (num - 1) % length;
            nums[x] += length;
        }

        //过滤小于等于数组长度的元素
        for (int i = 0; i < nums.length;i++) {
            if (nums[i] <= length) {
                res.add(i+1);
            }
        }
        return res;
    }





}
